/****************************************************************************
*                                                                           *
* Copyright (c) RSC. All rights reserved(2010).								*
* Developer: Marius Ionescu    						                        *
*                                                                           *
****************************************************************************/

using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web.Security;
using System.Web.UI.WebControls;
using NS_Controls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.UI;
using AjaxControlToolkit;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class UpdateRemote : SessionPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
			output.Text = Tools.HiddenExceptionDetails(ex);
		}
    }

    protected void LoadData()
    {
        try
        {
			TreeViewServer.Nodes.Clear();
			TreeNode rootServer = new TreeNode("Hipocrate.Net", "root");
			TreeViewServer.Nodes.Add(rootServer);
			AddChildren("~", rootServer, true, true);
			rootServer.CollapseAll();
			rootServer.Expand();

			cmdReloadLocalData_Click(null, null);
		}
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
			output.Text = Tools.HiddenExceptionDetails(ex);
		}
    }

	private static readonly string networkProtocol = "\\\\";
	protected void cmdReloadLocalData_Click(object sender, EventArgs e)
	{
		try
		{
			System.Security.Principal.WindowsImpersonationContext impContext = ImpersonationHelper.StartImpersonate();
			TreeViewLocal.Nodes.Clear();
			TreeNode rootLocal = new TreeNode("Updating Location", "root");
			TreeViewLocal.Nodes.Add(rootLocal);
			string uriCaller = networkProtocol + this.Request.UserHostAddress + "\\";
			if (txtUpdatingLocation.Text.StartsWith(networkProtocol))
				uriCaller = txtUpdatingLocation.Text;
			else
				uriCaller += txtUpdatingLocation.Text;
			AddChildren(uriCaller, rootLocal, true, false);
			ImpersonationHelper.StopImpersonate(impContext);
			rootLocal.CollapseAll();
			rootLocal.Expand();
		}
		catch (Exception ex)
		{
			this.SetMessage(ex.Message, true);
			output.Text = Tools.HiddenExceptionDetails(ex);
		}
	}

	protected void tvServer_CheckChanged(object sender, TreeNodeEventArgs e)
	{
		foreach (TreeNode child in e.Node.ChildNodes)
			SetNodeCheckState(child, e.Node.Checked);
	}
	private void SetNodeCheckState(TreeNode target, bool isChecked)
	{
		target.Checked = isChecked;
		foreach (TreeNode child in target.ChildNodes)
			SetNodeCheckState(child, isChecked);
	}

	private void AddChildren(string path, TreeNode target, bool recursive, bool useMapPath)
	{
		System.IO.DirectoryInfo parent = new System.IO.DirectoryInfo(useMapPath ? Server.MapPath(path) : path);
		foreach (System.IO.DirectoryInfo child in parent.GetDirectories())
		{
			TreeNode subnode = new TreeNode(child.Name, path + "/" + child.Name);
			target.ChildNodes.Add(subnode);
			if(recursive)
				AddChildren(path + "/" + child.Name, subnode, recursive, useMapPath);
		}
		foreach (System.IO.FileInfo file in parent.GetFiles())
			target.ChildNodes.Add(new TreeNode(file.Name, path + "/" + file.Name, GetFileIcon(file.Extension)));
		if(target.ChildNodes.Count == 0)
			target.ChildNodes.Add(new TreeNode("(empty)", ".", GetFileIcon(string.Empty)));
	}

	private string GetFileIcon(string extension)
	{
		return "";
	}

    protected void tabActive_TabChanged(object sender, EventArgs e)
    {
        
    }
}
